/**
 * KioskModel is the central model for the application.
 * 
 * @author		Scott Steven Rodriguez
 * @link 		http://www.flashsherpa.com
 */
package com.ettractions.kiosk.models
{
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.events.KioskModelEvent;
	import com.ettractions.kiosk.vo.AdVO;
	import com.ettractions.kiosk.vo.BrochureVO;
	import com.ettractions.kiosk.vo.CategoryVO;
	import com.ettractions.kiosk.vo.KioskVO;
	import com.ettractions.kiosk.vo.LocationVO;
	
	import flash.utils.Dictionary;
	
	import org.robotlegs.mvcs.Actor;
	
	public class KioskModel extends Actor
	{
		public var title:String;
		
		public var location:LocationVO;
		
		public var categories:Vector.<CategoryVO>;
		
		public var ads:Vector.<AdVO>;

		/**
		 * This is a bit confusing.  The "condensedBrochures" vector is the
		 * one-to-many brochure/category list as originally parsed from the
		 * XML by the data service.  It is used by the thumbnail grid view
		 * as the data source for the grid pages.  The "expandedBrochures"
		 * vector is the one-to-one brochure/category list generated when
		 * the model is populated and is used by the thumbnail ribbon and
		 * coverflow views as their data source.
		 */
		public var condensedBrochures:Vector.<BrochureVO>;
		public var expandedBrochures:Vector.<BrochureVO>;	
		
		/**
		 * Map of CategoryVO objects to int values that represent the index,
		 * in the expandedBrochures vector, of the first brochure in each
		 * category.
		 */		
		private var _categoryFirstBrochureIndeces:Dictionary = new Dictionary();
		
		private var _selectedBrochureIndex:int = -1;
		
		public function populate(data:KioskVO):void
		{
			var brochuresByCategory:Dictionary = new Dictionary();
			var category:CategoryVO;
			var brochure:BrochureVO;
			
			this.title = data.title;
			this.location = data.location;
			this.categories = data.categories;
			this.ads = data.ads;
			
			condensedBrochures = data.brochures;
			expandedBrochures = new Vector.<BrochureVO>();
			
			for each (category in categories)
			{
				brochuresByCategory[category] = new Vector.<BrochureVO>();
				
				for each (brochure in condensedBrochures)
				{
					if (brochure.categories.indexOf(category) > -1)
					{
						brochuresByCategory[category].push(brochure);
					}
				}
			}
			
			for each (category in categories)
			{
				_categoryFirstBrochureIndeces[category] = expandedBrochures.length;
				
				for each (brochure in brochuresByCategory[category])
					expandedBrochures.push(brochure);
			}
			
			eventDispatcher.dispatchEvent(new KioskModelEvent(KioskModelEvent.UPDATED));
		}
		
		public function set selectedBrochureIndex(index:int):void
		{
			if (_selectedBrochureIndex != index)
			{
				var brochure:BrochureVO = null;
				var category:CategoryVO = null;
				
				_selectedBrochureIndex = index;
				
				if (index > -1)
				{
					brochure = expandedBrochures[index];
					
					for each (var categoryVO:CategoryVO in categories)
						if (index >= _categoryFirstBrochureIndeces[categoryVO])
							category = categoryVO;
				}
				
				eventDispatcher.dispatchEvent(new BrochureSelectedEvent(BrochureSelectedEvent.BROCHURE_SELECTED, brochure, category, index));
			}
		}
		
		public function get selectedBrochureIndex():int
		{
			return _selectedBrochureIndex;
		}
		
		public function getCategoryFirstBrochureIndex(category:CategoryVO):uint
		{
			return _categoryFirstBrochureIndeces[category];				
		}
	}
}